<?php
class QuestionsController 
	extends Zend_Controller_Action 
{
	
	public function indexAction() 
	{
		$pagination = $this->getHelper('Pagination');
		
		$categoryId= $pagination->getVar('categoryId', null);
		
		$params = array();
		
		if( $categoryId ){
			$params['categoryId'] = $categoryId;
		}
		
		$mapper = Vs_Factory::mapper( 'Question' );
		$mapper2 = Vs_Factory::mapper( 'Category' );
		
		$questions = $mapper->getByParams( $params, $pagination->offset, $pagination->limit, $pagination->order );
		$totalItems = $mapper->countByParams( $params );
		
//		$mapper->loadCategory( $questions );
		
		$pagination->setItemsCount( $totalItems );
		
		$this->view->questions = $questions;
		$this->view->categories = $mapper2->getByParams();
		
		$this->view->questions->setCategory($this->view->categories);
		
		$this->view->categoryId = $categoryId;
		
		if ( $this->getRequest()->isXmlHttpRequest()){
			$this->_helper->layout->disableLayout();
			$this->render('questions');
		} 
		
	}
	
	public function createAction() 
	{
	    $session = Zend_Registry::get("session");
	    
		$mapper = Vs_Factory::mapper( 'Question' );
		$mapper2 = Vs_Factory::mapper( 'Category' );
		$mapper3 = Vs_Factory::mapper( 'City' );
		$mapper4 = Vs_Factory::mapper( 'ReasonFamily' );
		
		$form = new Adm_Form_Question(array(
			'categories' =>$mapper2->getByParams(),
			'cities' =>$mapper3->getByParams(),
			'reasonFamilies' =>$mapper4->getByParams()
		));
		$form->setAction(
			$this->_helper->url('create')
		);
		
		if($this->getRequest()->isPost()){
			
			$data = $this->getRequest()->getPost();
		
			if ( $form->isValid($data)) {

				$question = new Vs_Question ($data);
				$mapper->save( $question );				
			
				$session->lastCategoryId = $question->categoryId;
				
//				$this->getHelper('ActionLog')->log();
				
				$this->getHelper('MyFlashMessenger')->addSuccess( "Pregunta actualizada" );
				return $this->_helper->Redirector('index');
				
			} else {			
				$form->populate($data);	
			}	
		} else {
				
			if(isset($session->lastCategoryId)){
			    $form->getElement('categoryId')->setValue( $session->lastCategoryId );
			}
		}				
		
		$this->view->form = $form;
		$this->render('create');	
	}
	
	public function eliminarAction() 
	{
		$ids = explode("-", $this->getRequest()->getParam('ids'));
		
//		$this->getHelper('ActionLog')->log();
		
		$mapper = Vs_Factory::mapper( 'Question' );
		
		
		foreach ($ids as $questionId){
			
			try {
				$question = $mapper->getById( $questionId );
				$mapper->delete( $question );
				$this->getHelper('MyFlashMessenger')->addSuccess( "La pregunta {$questionId} fue eliminada" );
				
			}catch (Vs_Exception $e){
				$this->getHelper('MyFlashMessenger')->addError( $this->_helper->Translate($e->getMessage()) );
//				$this->getHelper('Log')->exception( $e );
			}
		
		} 
		
		return $this->_helper->Redirector('index');
	}
	
	public function updateAction() 
	{
		try {
			$questionId = (int) $this->_getParam('questionId');
		
			$mapper = Vs_Factory::mapper( 'Question' );
			$mapper2 = Vs_Factory::mapper( 'Category' );
			$mapper3 = Vs_Factory::mapper( 'City' );
			$mapper4 = Vs_Factory::mapper( 'ReasonFamily' );
			
			try{
				$question = $mapper->getById( $questionId);
			} catch (Vs_Exception $e){		
				$this->getHelper('MyFlashMessenger')->addError( $e->getMessage() );
//				$this->getHelper('Log')->exception( $e );
				return $this->_helper->Redirector('index');
			}
			
			$form = new Adm_Form_Question(array(
				'categories' =>$mapper2->getByParams(),
				'cities' =>$mapper3->getByParams(),
				'reasonFamilies' =>$mapper4->getByParams()
			));
			$form->setAction(
				$this->_helper->url('update', null, null, array('questionId'=>$questionId))
			);
			
			if($this->getRequest()->isPost()){
				
				$data = $this->getRequest()->getPost();
			
				if ( $form->isValid( $data )) {
					
					$data = $form->getValues();
					
					$data['questionId'] = $questionId;
					$question = new Vs_Question ($data);
					$mapper->save( $question );				
				
//					$this->getHelper('ActionLog')->log();
					
					$this->getHelper('MyFlashMessenger')->addSuccess( "Pregunta {$questionId} modificada" );
					return $this->_helper->Redirector('index');
					
				} else {		
	
					$form->populate($data);			
				}	
			}  else {			
				$form->populate( (array) $question );			
			}					
			
				
				
		}catch (Vs_Exception $e){
			$this->getHelper('MyFlashMessenger')->addError( $this->_helper->Translate($e->getMessage()) );
//			$this->getHelper('Log')->exception( $e );
		}
		
		$this->view->form = $form;
		$this->render('create');	
		
	}
	
}